package com.ssm.controller;

import com.ssm.Utils.QRUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;

@Controller
@CrossOrigin
public class QRController {
    @RequestMapping(value = "getQRCode",method = RequestMethod.GET)
    public void getQRCode(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //获取前台传入参数
        String content = request.getParameter("content");
        //String content = "http://www.youdao.com/";
        String imgPath = "/Users/sanmu/Desktop/__logo.jpg";
        Boolean needCompress = true;
        //通过调用工具类，拿到图片流
        ByteArrayOutputStream out = QRUtils.encodelIO(content,imgPath,needCompress);
        //定义返回参数
        response.setCharacterEncoding("UTF-8");
        response.setContentType("image/jpeg;charset=UTF-8");
        response.setContentLength(out.size());
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(out.toByteArray());
        outputStream.flush();
        outputStream.close();
    }
}
